package boundary;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;

import client.Client;
import entity.CasualSubscription;
import entity.Customer;
import entity.FullSubscription;
import entity.OneTimeCostumerOrder;
import entity.Order;
import entity.Prices;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;

public class CheckoutGui extends JPanel{
	JFrame frame;
	JPanel lastPanel;
	private JTextField textField;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	public CheckoutGui() {
		
		setLayout(null);
		
		JLabel lblEnterCarNumber = new JLabel("enter car number for checkout");
		lblEnterCarNumber.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblEnterCarNumber.setBounds(92, 33, 301, 36);
		add(lblEnterCarNumber);
		
		JLabel lblCarNumber = new JLabel("car number");
		lblCarNumber.setBounds(68, 134, 75, 14);
		add(lblCarNumber);
		
		textField = new JTextField();
		textField.setBounds(146, 131, 158, 20);
		add(textField);
		textField.setColumns(10);
		
		JButton btnCheckout = new JButton("checkout");
		btnCheckout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				String carNum=textField.getText();
				int kind = 0;	//kind of customer (subscriber/one time...)
				int index = 0;	//index in the array
				Date curTime=new Date();
				FullSubscription [] fullSubs=Client.getFullSubscriptions();	//kind 1
				CasualSubscription [] casualSubs=Client.getCasualSubscriptions();	//kind 2
				OneTimeCostumerOrder [] oneTimeCustomerOrders=Client.getOneTimeCustomerOrders();	//kind 3
				Order [] orders=Client.getOrders();	//kind 4
				
				for (int i=0; i<fullSubs.length; i++)
				{
					if (fullSubs[i].getCar_number().equals(carNum) && fullSubs[i].getStart_date().compareTo(curTime)<0 && fullSubs[i].getEnd_date().compareTo(curTime)>0 )
					{
						kind=1;
						index=i;
						break;
					}
				}
				for (int i=0; i<casualSubs.length; i++)
				{
					if (casualSubs[i].getCar_number().equals(carNum) && casualSubs[i].getStart_date().compareTo(curTime)<0 && casualSubs[i].getEnd_date().compareTo(curTime)>0 && casualSubs[i].getParking_lot_number()==Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum())
					{
						kind=2;
						index=i;
						break;
					}
				}
				for (int i=0; i<oneTimeCustomerOrders.length; i++)
				{
					if (oneTimeCustomerOrders[i].getCar_number().equals(carNum) && oneTimeCustomerOrders[i].getEntering_hour().compareTo(curTime)<0 && oneTimeCustomerOrders[i].getActual_leaving_hour()==null && oneTimeCustomerOrders[i].isPayment_status()==false)
					{
						kind=3;
						index=i;
						break;
					}
				}
				for (int i=0; i<orders.length; i++)
				{
					if(orders[i].isCanceled()==false && orders[i].getCarNumber().equals(carNum) && orders[i].getParkingLotNumber()==Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum() && orders[i].getActualArrivalDate()!=null && orders[i].getActualLeavingDate()==null)
					{
						kind=4;
						index=i;
						break;
					}
				}
				if(Client.getParkingLots_sorted()[Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum()-1].popCar(carNum))
					switch(kind)
					{
					case 1:	//full subscription
						JOptionPane.showMessageDialog(null,"this car is a subscriber, dont take money","full subscription checkout",JOptionPane.INFORMATION_MESSAGE);
						break;
					case 2:	//casual subscription
						JOptionPane.showMessageDialog(null,"this car is a subscriber, dont take money","casual subscription checkout",JOptionPane.INFORMATION_MESSAGE);
						break;
					case 3:	//one time customer order
						int payment=0;
						final Prices p=Client.getCurrentPrices(Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum());
						oneTimeCustomerOrders[index].setActual_leaving_hour(new Date());
						oneTimeCustomerOrders[index].setPayment_status(true);
						payment+=((oneTimeCustomerOrders[index].getActual_leaving_hour().getTime() - oneTimeCustomerOrders[index].getEntering_hour().getTime())/(1000*60*60))*p.getOccasionalParkingPlace();
						JOptionPane.showMessageDialog(null,"this car is an occational customer.\ncharge "+payment+"$ in cash.","one time costumer checkout",JOptionPane.INFORMATION_MESSAGE);

						break;
					case 4:	//regular order
						int pay=0;
						final Prices p1=Client.getCurrentPrices(Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum());
						Customer customers[]=Client.getCustomers();
						Customer customer = null;
						for (int i=0; i<customers.length; i++)
							if (customers[i].getCostumer_id()==orders[index].getCustomerID())
								customer=customers[i];
						orders[index].setActualLeavingDate(new Date());
						if (orders[index].getActualLeavingDate().compareTo(orders[index].getLeavingDate())>0)	//its late to exit so charge by higher rate
							pay+=((orders[index].getActualLeavingDate().getTime() - orders[index].getLeavingDate().getTime())/(1000*60*60))*p1.getOccasionalParkingPlace();
						else	//give him back the money if its early (by reguler rate)
							pay+=((orders[index].getActualLeavingDate().getTime() - orders[index].getLeavingDate().getTime())/(1000*60*60))*p1.getOrderedParkingPlace();
						customer.AddBalance(-pay);
						JOptionPane.showMessageDialog(null,"this car is a one time ordered customer.\n"+pay+"$ was charged in the system.\nlet him go!","one time ordered checkout",JOptionPane.INFORMATION_MESSAGE);

						break;
					case 0:
						JOptionPane.showMessageDialog(null,"checkin problem- check for order history","problem",JOptionPane.INFORMATION_MESSAGE);

						break;
					}
				else
					JOptionPane.showMessageDialog(null,"there is a problem finding this car, call johnson for tech support","error",JOptionPane.ERROR_MESSAGE);

			}
		});
		btnCheckout.setBounds(92, 236, 89, 23);
		add(btnCheckout);
		
		JButton btnCancel = new JButton("cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setContentPane((JPanel)lastPanel);
			}
		});
		btnCancel.setBounds(266, 236, 89, 23);
		add(btnCancel);
	}
}
